from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated
from dhm.models.return_goods.return_goods import ReturnGoods

class ReturnGoodsView(APIView):
    permission_classes = ([IsAuthenticated])
    def get(self, request):
        photo = request.GET.get('photo', '')
        price = int(request.GET.get('price', 1))
        title = request.GET.get('title', '')
        size = request.GET.get('size', '')
        consumer_id = int(request.GET.get('consumer_id', 1))
        consumer_name = request.GET.get('consumer_name', '')
        consumer_address = request.GET.get('consumer_address', '')
        return_time = request.GET.get('return_time', '')
        purchase_number = int(request.GET.get('purchase_number', 1))
        purchase_color = request.GET.get('purchase_color', '')
        reason = request.GET.get('reason', '')
        returngoodses = ReturnGoods.objects.all().order_by('-return_time')
        data = []
        for returngoods in returngoodses:
            data.append({
                'id': returngoods.id,
                'photo': returngoods.photo,
                'price': returngoods.price,
                'title': returngoods.title,
                'size': returngoods.size,
                'consumer_id': returngoods.consumer_id,
                'consumer_name': returngoods.consumer_name,
                'consumer_address': returngoods.consumer_address,
                'return_time': returngoods.return_time,
                'purchase_number': returngoods.purchase_number,
                'purchase_color': returngoods.purchase_color,
                'reason': returngoods.reason,
            })
        return Response(data)

    def post(self, request):
        data = request.POST
        photo = data.get('photo', '')
        price = int(data.get('price', 1))
        title = data.get('title', '')
        size = data.get('size', '')
        consumer_id = int(data.get('consumer_id', 1))
        consumer_name = data.get('consumer_name', '')
        consumer_address = data.get('consumer_address', '')
        return_time = data.get('return_time', '')
        purchase_number = data.get('purchase_number', '')
        purchase_color = data.get('purchase_color', '')
        reason = data.get('reason', '')
        ReturnGoods.objects.create(photo=photo, price=price, title=title, size=size, consumer_id=consumer_id, consumer_name=consumer_name, consumer_address=consumer_address, return_time=return_time,purchase_number=purchase_number, purchase_color=purchase_color, reason=reason)
        return Response({
            'result': "success"
        })
